package com.knight.jdk8.biFunction;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
 * @Description: BiFunction测试
 * @author: knight
 * @Date: 2019-04-21 18:31
 */
public class BiFunctionTest {
    public static void main(String[] args) {
        Person person1 = new Person("zhangsan", 20);
        Person person2 = new Person("lisi", 30);
        Person person3 = new Person("wangwu", 40);

        List<Person> personList = Arrays.asList(person1, person2, person3);


        List<Person> findUserNamePersons = findByUserName("zhangsan", personList);
        findUserNamePersons.forEach(System.out::println);

        System.out.println("----------------------");

        findByAge(20,personList).forEach(System.out::println);

    }

    /**
     * 通过username查询
     *
     * @param username 指定的查询条件
     * @param persons
     * @return
     */
    private static List<Person> findByUserName(String username, List<Person> persons) {
        return persons.stream().filter(person -> person.getName().equals(username)).collect(Collectors.toList());
    }

    /**
     * 通过biFunction实现
     *
     * @param age
     * @param persons
     * @return
     */
    private static List<Person> findByAge(Integer age, List<Person> persons) {

        BiFunction<Integer, List<Person>, List<Person>> biFunction =
                (ageOfPerson, listPerson) -> listPerson.stream().filter(person -> person.getAge() > age).collect(Collectors.toList());

        return biFunction.apply(age, persons);
    }

}
